package org.todo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.PortletRequestUtils;
import org.springframework.web.portlet.mvc.AbstractController;
import org.todo.domain.Category;
import org.todo.domain.Todo;
import org.todo.service.CategoryService;
import org.todo.service.TodoService;

/**
 * Controller that handles action and render requests for displaying todos and their categories
 * @author Matthias Braunhofer
 */
public class TodosController extends AbstractController {
    //injected dependencies
    private CategoryService categoryService;
    private TodoService todoService;

    /**
     * Action handler method that handles the request to display the todos of a certain category
     * @param request
     * @param response
     * @throws java.lang.Exception
     */
    @Override
    protected void handleActionRequestInternal(ActionRequest request, ActionResponse response) throws Exception {
        int categoryId = PortletRequestUtils.getIntParameter(request, "categoryId", -1);
        request.getPortletSession().setAttribute("selectedCategory", categoryId, PortletSession.PORTLET_SCOPE);
        request.getPortletSession().removeAttribute("searchCommand");
    }

    /**
     * Handler method that handles the rendering of todo and category items
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    @Override
    protected ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception {
        String username = ControllerUtil.getUserName(request);

        int categoryId = ControllerUtil.getSelectedCategoryFromSession(request);
        Object searchCommand = request.getPortletSession().getAttribute("searchCommand");

        Map model = new HashMap();

        if (searchCommand != null) {
            //a search has been performed: show the search results
            SearchCommand searchCmd = (SearchCommand)searchCommand;
            List<Todo> todos = todoService.searchTodos(username, searchCmd);

            model.put("mode", "todos");
            model.put("todos", todos);
            model.put("searchText", searchCmd.getSearchText());
        } else if (categoryId > 0) {
            //a category has been selected: show all its containing todo items
            Category category = categoryService.getCategory(categoryId);
            List<Todo> todos = todoService.getTodos(categoryId);
           
            model.put("mode", "todos");
            model.put("todos", todos);
            model.put("selectedCategoryName", category.getName());
        } else {
            //categories mode is the default mode
            List<Category> categories = categoryService.getCategories(username);
            
            model.put("mode", "categories");
            model.put("categories", categories);
        }

        model.put("selectedCategory", categoryId);

        return new ModelAndView("todoList", model);
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public void setTodoService(TodoService todoService) {
        this.todoService = todoService;
    }
}
